package GUI;

import 数据库和网络.myDatebase;
import 试题和试卷.paper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Paper;
import java.sql.SQLException;
import java.util.ArrayList;

public class paperItem extends JFrame {

    JLabel num,paperLabel;
    JButton detailButton,doButton;
    public paper currentpaper;
    int leftTime;

    public paperItem(){}

    public paperItem(int n,paper currentpaper){
        this.currentpaper=currentpaper;
        num = new JLabel(""+n);
        int loc ;
        if(n%5!=0){
            loc=n%5;
        }else {
            loc=5;
        }
        setTitleType(num);
        paperLabel = new JLabel("试卷名："+currentpaper.getName()+"  ");
        /*先暂且只写了试卷名*/
        setTextType(paperLabel);
        num.setBounds(80,120+50*loc,60,50);
        paperLabel.setBounds(140,120+50*loc,600,50);
        detailButton = new JButton("显示详情");
        doButton = new JButton("开始做题");
        setButtonType(detailButton);
        setButtonType(doButton);
        detailButton.setBounds(740,120+50*loc,80,50);
        doButton.setBounds(820,120+50*loc,80,50);
        detailButton.addActionListener(new detailListener());
        doButton.addActionListener(new startListener());

        myDatebase timequery=new myDatebase("result_record");
        String sql=String.format("select result_record.%s.remainTime from result_record.%s where studentID=%s",
                currentpaper.getName(),currentpaper.getName(),enterWindow.currentStudent.getSchoolID());
        try {
            timequery.statement.execute(sql);
            timequery.resultSet=timequery.statement.executeQuery(sql);
            while(timequery.resultSet.next()){
                int  iet=currentpaper.getTime();
                if(timequery.resultSet.getString("remainTime")!=null)
                    iet=Integer.parseInt(timequery.resultSet.getString("remainTime"));
                currentpaper.setRemainTime(iet);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            timequery.close();
        }

        if(currentpaper.getRemainTime()==0){
            doButton.setEnabled(false);
        }
    }

    public void setTitleType(JLabel label){
        Font bigFont = new Font("sanserif",Font.PLAIN,16);
        label.setBackground(Color.lightGray);
        label.setFont(bigFont);
        label.setOpaque(true);
        label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setTextType(JLabel label){
        Font smallFont = new Font("sanserif",Font.PLAIN,12);
        label.setBackground(Color.white);
        label.setOpaque(true);
        label.setFont(smallFont);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setButtonType(JButton button){
        Font smallFont = new Font("sanserif",Font.PLAIN,14);
        //button.setBackground(Color.GRAY);
        button.setFont(smallFont);
        button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void add(){
    }

    public class detailListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            new detailWindow(currentpaper);
        }
    }

    public class startListener implements  ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){

            myDatebase timequery=new myDatebase("result_record");
            String sql=String.format("select result_record.%s.remainTime from result_record.%s where studentID=%s",
                    currentpaper.getName(),currentpaper.getName(),enterWindow.currentStudent.getSchoolID());
            try {
                timequery.statement.execute(sql);
                timequery.resultSet=timequery.statement.executeQuery(sql);
                while(timequery.resultSet.next()){
                    int  iet=currentpaper.getTime();
                    if(timequery.resultSet.getString("remainTime")!=null)
                        iet=Integer.parseInt(timequery.resultSet.getString("remainTime"));
                    currentpaper.setRemainTime(iet);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                timequery.close();
            }

            if(currentpaper.getRemainTime()==0){
                JFrame unfinished=new JFrame();
                JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                JLabel successLab=new JLabel();
                JLabel lab2=new JLabel(" ");
                unfinished.setSize(200,150);
                Font bigFont = new Font("sanserif",Font.PLAIN,15);
                String a="答题时间用完了！";
                successLab.setText(a);
                successLab.setFont(bigFont);
                panel.setSize(unfinished.getSize());
                panel.add(lab2);
                panel.add(successLab);
                unfinished.getContentPane().add(BorderLayout.CENTER,panel);
                unfinished.setLayout(new FlowLayout());//流式布局
                unfinished.setLocationRelativeTo(null);//居中

                unfinished.setTitle("在线考试平台");
                unfinished.setVisible(true);
                return;
            }



             javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run()
                {
                    test.ansboard=new answerSheet(currentpaper.getRemainTime(),currentpaper.getQuestionNumber(),currentpaper.getSelectionNumber(),currentpaper.getDefinitionNumber(),currentpaper.getFillingBlankNumber(),currentpaper.getsubjectiveNumber());
                }
            });

            Thread tse=new Thread(new Runnable(){
                public void run(){test.demo=new test(currentpaper);}
            }) ;
            tse.start();




        }
    }
}

